use crate::backend::Backend;
use crate::ir::IR;

pub struct Rockchip{}


impl Backend for Rockchip{
    fn gen(&self, ir: &[IR]) -> String {
        let mut out_str = String::new();
        for (i, line) in ir.iter().enumerate() {
            match line {
                IR::Transfer { cmd, data } => {
                    let count = 1 + data.len();
                    let write_cmd = match data.len() {
                        0 => { 0x05 }
                        1 => { 0x15 }
                        _ => { 0x39 }
                    };
                    let mut delay = 0;

                    if i + 1 < ir.len() {
                        if let IR::Delay(d) = ir[i+1] {
                            delay = d.as_millis()
                        }
                    }

                    let mut paras = String::new();
                    for p in data {
                        paras = format!("{paras} {:02X}", p);
                    }
                    out_str = format!("{}{:02X} {:02X} {:02X} {:02X}{paras}\n", out_str,
                                      write_cmd, delay, count, cmd);
                }
                IR::Delay(_) => continue
            }
        }


        out_str
    }
}